-- 崩溃处理
if HasModName("错误追踪") then
    return
end


local s_util = require "libs/settingmanager"
local id = "last_save_slot"
AddClassPostConstruct("screens/redux/servercreationscreen", function(self)
    local func = self.create_button and self.create_button.onclick
    if type(func) == "function" then
        self.create_button.onclick = function(...)
            local s_slot = self.save_slot
            if type(s_slot) == "number" then
                s_util:SaveSettingLine(id, {slot = s_slot})
            end
            return func(...)
        end
    end
end)
local Menu = require "widgets/menu"
AddClassPostConstruct("widgets/scripterrorwidget", function(self, title, text, buttons, texthalign, additionaltext, ...)
    local is_client = GLOBAL.TheNet and GLOBAL.TheNet:GetIsClient() and GLOBAL.TheNet:GetIsServerAdmin()
    local data = s_util:GetSettingLine(id)
    local slot = type(data.slot) == "number" and data.slot
    local flag,is_local
    local data_text_1 = "服务器日志󰀔"
    local data_text_2 = "客户端日志󰀝"
    for id, button in pairs(buttons)do
        if type(button) == "table" and button.text == GLOBAL.STRINGS.UI.MAINSCREEN.MODFORUMS then
            flag = true
            buttons[id].text = data_text_2
            buttons[id].cb = function()
                TheSim:OpenDocumentsFolder()
            end
            break
        end
    end
    if is_client and slot then
        local btn = {text = data_text_1}
        if slot < 100000 then     -- 大于10w是云存储，云存的话日志看继续看本地
            btn.cb = function()
                TheSim:OpenSaveFolder(slot)
            end
            is_local = true
        else
            btn.cb = function()
                TheSim:OpenDocumentsFolder()
            end
        end
        table.insert(buttons, btn)
    end
    if self.menu and flag then
        self.menu:Hide()    -- 其实Kill也行
        self.menu = self.root:AddChild(Menu(buttons, 250, true))
        for _, btn in pairs(self.menu.items)do
            if btn.name == data_text_1 then
                if is_local then
                    btn:SetHoverText("点击此按钮会显示一些文件夹,\n地面文件夹：Master，洞穴文件夹：Caves\n打开对应的文件夹，就能看到服务器的日志文件了,\n 文件名是：server_log", {offset_y = 120})
                else
                    btn:SetHoverText("点击此按钮会打开一个文件夹,\n按下backspace或者手动返回上一层目录,\n就能看到服务器的日志文件了,\n 地表日志文件：master_server_log\n洞穴日志文件：caves_server_log", {offset_y = 150})
                end
            elseif btn.name == data_text_2 then
                btn:SetHoverText("点击此按钮会打开一个文件夹,\n按下backspace或者手动返回上一层目录,\n就能看到客户端的日志文件了,\n 文件名是：client_log", {offset_y = 120})
            end
        end
        self.menu:SetHRegPoint(GLOBAL.ANCHOR_MIDDLE)
        self.menu:SetPosition(0, -250, 0)
        self.default_focus = self.menu
        if self.additionaltext then
            local str = self.additionaltext.string
            local match_str = GLOBAL.STRINGS.UI.MAINSCREEN.SCRIPTERRORMODWARNING.."(.*)"
            local text = string.match(str, match_str)
            if text then
                self.additionaltext:SetString("此错误可能是由于你启用的某个模组所致！详细信息请点击下方按钮！\n 你启用了这些模组：\n"..text)
            end
        end
    end
end)